package com.yl.chainStore.entity.sick

import com.alibaba.druid.support.json.JSONUtils
import com.alibaba.fastjson.JSONObject
import com.baomidou.mybatisplus.annotation.IdType
import com.baomidou.mybatisplus.annotation.TableField
import com.baomidou.mybatisplus.annotation.TableId
import com.baomidou.mybatisplus.annotation.TableName
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler
import com.yl.chainStore.common.annotations.CompanyUserOperationLogColumn
import com.yl.chainStore.common.enums.TypeEnum
import com.yl.chainStore.entity.OtherShow
import com.yl.chainStore.util.dayFormat
import com.yl.chainStore.util.yearFormat
import com.yl.common.annotation.NoArg
import com.yl.common.getContext
import org.apache.ibatis.type.JdbcType
import java.util.*

/**
 * <p>
 * 病历
 * </p>
 *
 * @author cxw
 * @since 2021-07-10
 */
@NoArg
@TableName(value = "sick_medical_history",autoResultMap = true)
data class SickMedicalHistory(

	/**
	 * id
	 */
	@TableId(value = "id", type = IdType.AUTO)
	var id: Long,

	/**
	 * 创建时间
	 */
	@TableField("create_time")
	var createTime: Date = Date(),


	/**
	 * 创建人
	 */
	@TableField("create_user_id")
	var createUserId: Long = getContext().userId,

	/**
	 * 客户
	 */
	@TableField("customer_id")
	var customerId: Long,

	/**
	 * 类型 初诊，复诊
	 */
	@TableField("type")
	@CompanyUserOperationLogColumn(value = "就诊类型",columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_TYPE_ENUM)
	var type: TypeEnum ?= null,

	/**
	 * 公司id
	 */
	@TableField("company_id")
	var companyId: Long= getContext().companyId,

	/**
	 * 分院id
	 */
	@TableField("dept_id")
	var deptId:Long = getContext().subCompanyId,

	/**
	 * 就诊记录id
	 */
	@TableField("record_id")
	var recordId:Long ?= null,

	/**
	 *
	 */
	@TableField("doctor_id")
	@CompanyUserOperationLogColumn(value = "医生",columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_COMPANY_USER)
	var doctorId:Long ?= null,

	/**
	 * 医生姓名
	 */
	@TableField(exist = false)
	var doctorName: String ?= null,

	@TableField("nurse_id")
	@CompanyUserOperationLogColumn(value = "护士",columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_COMPANY_USER)
	var nurseId:Long ?= null,

	/**
	 * 护士姓名
	 */
	@TableField(exist = false)
	var nurseName: String? = null,

	/**
	 * 就诊时间
	 */
	@TableField("see_doctor_time")
	@CompanyUserOperationLogColumn(value = "就诊时间",columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_DATE_TIME )
	var seeDoctorTime:Date = Date(),

	/**
	 * 标签
	 */
	@TableField("label_ids")
	@CompanyUserOperationLogColumn(value = "标签",columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_SICK_LABEL)
	var labelIds : String ?= null,

	/**
	 * 备注
	 */
	@TableField("remark")
	@CompanyUserOperationLogColumn(value = "备注")
	var remark:String?=null,

	/**
	 * 主诉
	 */
	@TableField("action_chief")
	@CompanyUserOperationLogColumn(value = "主诉")
	var actionChief: String ?= null,

	@TableField("update_time")
	var updateTime:Date = Date(),

	@TableField("update_user_id")
	var updateUserId:Long = getContext().userId
){

	@TableField(value = "other",jdbcType= JdbcType.OTHER,typeHandler = FastjsonTypeHandler::class)
	@CompanyUserOperationLogColumn(value = "内容",columnType = TypeEnum.COMPANY_USER_OPERATION_LOG_COLUMN_JSON)
	var other:Any?=null
		set(value) {
			field = if(value is Map<*, *>){
				value as Any
			}else{
				value
			}
		}


	val otherJson: JSONObject?
		get() {
			if(other != null){
				return if(other is Map<*, *>){
					JSONObject.parseObject(JSONUtils.toJSONString(other))
				}else{
					JSONObject.parseObject(other.toString())
				}
			}
			return null
		}



	var labelIdsShow:List<Long>?
		get() {
			if(labelIds.isNullOrEmpty()){
				return null
			}
			val split = labelIds!!.split(",")
			val re = mutableListOf<Long>()
			for(s in split){
				if(s.isEmpty()){
					continue
				}
				re.add(s.toLong())
			}
			return re
		}
		set(value){
			val str  = StringBuffer()
			if(!value.isNullOrEmpty()){
				for(i in value){
					str.append("$i,")
				}
			}
			labelIds = str.toString()

		}


	val showYear:String
		get() {
			return yearFormat(seeDoctorTime)
		}

	val showDay:String
		get() {
			return dayFormat(seeDoctorTime)
		}
}
